﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.ReceptionGoods
{
    public partial class NaklPropertys : Form
    {
        Int32 _docNumber;
        Int32 _contragentId;
        Int32 _origId;
        DateTime _origMd;
        public NaklPropertys(Int32 docNumber)
        {
            _docNumber = docNumber;
            InitializeComponent();
        }

        private void fillToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
               // this.docInfoTableAdapter.Fill(this.util.DocInfo, ((int)(System.Convert.ChangeType(idToolStripTextBox.Text, typeof(int)))));
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        private void cbContragentsInit()
        {
            DataSets.SettingsTableAdapters.ContragentsTableAdapter adapter = new DIProject.DataSets.SettingsTableAdapters.ContragentsTableAdapter();
            DataSets.Settings.ContragentsDataTable cdt = adapter.GetData();
           // DataSets.Settings.ContragentsRow cr = cdt.NewContragentsRow();
          //  cr.Id = -100;
          //  cr.Name = "Выберите поставщика";
         //   cdt.Rows.InsertAt(cr, 0);
            cbContragents.DisplayMember = "Name";
            cbContragents.ValueMember = "Id";
            cbContragents.DataSource = cdt;
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            DateTime? contrDocDate = null;
            if(contragentDocDateDateTimePicker.Checked)
                contrDocDate = contragentDocDateDateTimePicker.Value;
            DateTime? payDate = null;
            if(paymentDateDateTimePicker.Checked)
                payDate=paymentDateDateTimePicker.Value;
            Data.Nkld.SupplierInvoiceDocUpdate(
                docDateDateTimePicker.Value,
                docNumberTextBox.Text,
                _contragentId,
                contrDocDate,
                contragentDocNumberTextBox.Text,
                payDate,
                noteTextBox.Text,
                DateTime.Now,
                FirstForm.Login,
                _origId,
                _origMd);
        }
        DataSets.Util.DocInfoRow docRow;
        private void NaklPropertys_Load(object sender, EventArgs e)
        {
            cbContragentsInit();
            DataSets.UtilTableAdapters.DocInfoTableAdapter adapter = new DIProject.DataSets.UtilTableAdapters.DocInfoTableAdapter();
            adapter.Fill(this.util1.DocInfo, _docNumber);
            if(this.util1.DocInfo.Count !=1)
            {
                throw new Util.DIException("Ошибка при получении свойств документа");
            }
            docRow = this.util1.DocInfo[0];
            _origId = docRow.Id;
            _origMd = docRow.MD;
            _contragentId = docRow.ContragentId;
            cbContragents.SelectedValue = _contragentId;
        }

        private void cbContragents_SelectedIndexChanged(object sender, EventArgs e)
        {
            _contragentId = (Int32)cbContragents.SelectedValue;
        }
    }
}